package com.rongzhe.house.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongzhe.house.R;
import com.rongzhe.house.entity.voo.AppoiintmentInfo;
import com.rongzhe.house.entity.voo.OrderVo;
import com.rongzhe.house.manager.DictionaryManager;
import com.rongzhe.house.utils.UIUtils;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by huang on 2018/1/4.
 */

public class OrderAdapter extends BaseAdapter {
    private List<OrderVo> mDatas;
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private View.OnClickListener mPayNowClickListener;
    private static String THUMBNAIL_SUFFIX = null;
    private static final String THUMBNAIL_SUFFIX_FORMAT = "imageView2/1/w/%s/h/%s";

    public OrderAdapter(Context context, View.OnClickListener payNowClickListener) {
        mContext = context;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        mPayNowClickListener = payNowClickListener;
    }

    public void setDatas(List<OrderVo> datas) {
        mDatas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas == null ? null : mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.list_item_order, parent, false);
        }
        MyViewHolder holder = (MyViewHolder) convertView.getTag();
        if (holder == null) {
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        }
        final OrderVo orderVo = (OrderVo) getItem(position);
        if (TextUtils.isEmpty(THUMBNAIL_SUFFIX)) {
            THUMBNAIL_SUFFIX = String.format(THUMBNAIL_SUFFIX_FORMAT,
                    String.valueOf(UIUtils.dip2px(mContext, 107)),
                    String.valueOf(UIUtils.dip2px(mContext, 75)));
        }
        if (orderVo.getContent() != null) {
            holder.textName.setText(orderVo.getContent().getName());
            Picasso.with(mContext).load(orderVo.getContent().getCover()
                    + "?" + THUMBNAIL_SUFFIX).into(holder.imageHousePhoto);
        } else {
            holder.textName.setText("-");
            holder.imageHousePhoto.setImageResource(R.drawable.default_house_photo);
        }
        holder.textPrice.setText(orderVo.getRemark());
        holder.textOrder.setText("订单编号:" + orderVo.getCode());
        String orderStateText = null;
        if(orderVo.getStatus() == OrderVo.Companion.getSTATUS_FINISHED()) {
            orderStateText = "订单已完成";
            holder.textOrderStatus.setTextColor(mContext.getResources()
                    .getColor(R.color.color_primary));
            holder.payNow.setVisibility(View.GONE);
        } else {
            orderStateText = "订单状态:等待支付";
            holder.textOrderStatus.setTextColor(mContext.getResources()
                    .getColor(R.color.font_color_new_hint2));
            holder.payNow.setVisibility(View.VISIBLE);
            holder.payNow.setTag(orderVo.getCode());
            holder.payNow.setOnClickListener(mPayNowClickListener);
        }
        holder.textOrderStatus.setText(orderStateText);
        holder.textMoney.setText("￥" + orderVo.getAmount() + "元");
        return convertView;
    }

    class MyViewHolder {
        View root;
        @BindView(R.id.text_order)
        TextView textOrder;
        @BindView(R.id.text_order_status)
        TextView textOrderStatus;
        @BindView(R.id.image_house_photo)
        ImageView imageHousePhoto;
        @BindView(R.id.text_house_name)
        TextView textName;
        @BindView(R.id.text_price)
        TextView textPrice;
        @BindView(R.id.text_money)
        TextView textMoney;
        @BindView(R.id.btn_pay_now)
        Button payNow;

        public MyViewHolder(View itemView) {
            root = itemView;
            ButterKnife.bind(this, itemView);
        }
    }
}
